package com.frost.springbook.model;

import lombok.Data;

@Data
public class Result<T> {
    private Integer code; // 后端响应状态, 业务状态码 200-成功 1-失败 2-未登录
    private String errmsg; // 后端发生错误的原因

    private T data;

    /**
     * 成功时
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(200);
        return result;
    }
    /**
     * 失败
     */
    public static <T> Result<T> fail(T data, String errmsg) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(-1);
        result.setErrmsg(errmsg);
        return result;
    }
    public static <T> Result<T> fail(String errmsg) {
        Result<T> result = new Result<>();
        result.setCode(-1);
        result.setErrmsg(errmsg);
        return result;
    }
    /**
     * 未登录
     */
    public static <T> Result<T> unlogin() {
        Result<T> result = new Result<>();
        result.setCode(-2);
        result.setErrmsg("用户未登录");
        return result;
    }
}
